package com.tianji.contract.service.Impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.common.Result;
import com.tianji.contract.bill.task.BillTask;
import com.tianji.contract.mapper.ParkBillMapper;
import com.tianji.contract.pojo.ParkBill;
import com.tianji.contract.service.ParkBillService;
import com.tianji.exception.BizException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.concurrent.DelayQueue;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈佳
 * @since 2024-10-15
 */
@Service
@RequiredArgsConstructor
public class ParkBillServiceImpl extends ServiceImpl<ParkBillMapper, ParkBill> implements ParkBillService {

    private final DelayQueue<BillTask> delayQueue;

    private final ParkBillMapper parkBillMapper;

    @Override
    public Result crearteBill(Long id) {
        //更新账单状态
        ParkBill parkBill = parkBillMapper.selectById(id);
        if(parkBill == null){
            throw new BizException("账单不存在");
        }
        boolean update = lambdaUpdate().eq(ParkBill::getId, id).set(ParkBill::getStatus, "PAID").update();
        if(update){
            long delayInMilliseconds = 15 * 60 * 1000; // 15分钟的毫秒数
            BillTask billTask = new BillTask(id, delayInMilliseconds);
            delayQueue.put(billTask);
        }
        return Result.success("账单已经确认");
    }
}
